#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   setting
@Time    :   2023/04/06 21:04:11
@author  :   Eval
@Version :   1.0
@Describe:   游戏设置
'''
from pygame.locals import *
from lib.scripts.GC import *
from lib.scripts.ui.ShowDiaLog import *
import sys

class Setting:
    def __init__(self,parent) -> None:
        import main
        self.parent:main.Run = parent
        self.packBG = pygame.Surface((200,300))
        self.packBG.set_alpha(170)
        self.__isActive = False # 设置ui是否激活, 禁止从外部直接修改
        self.BtnGroups = {
                "closeDuel":{
                    "name":"关闭/开启战斗过程",
                    "function":self.parent.GameDuel.changeState,
                    "pos":None,
                    "index":0  # 动态设置
                },
                "changeSound":{
                    "name":"关闭/开启音效",
                    "function":self.parent.GameMusic.ModifyPlaySound,
                    "pos":None,
                    "index":0
                },
                "changeMusic":{
                    "name":"关闭/开启BGM",
                    "function":self.parent.GameMusic.ModifyPlayBGM,
                    "pos":None,
                    "index":0
                },
                "quit":{
                    "name":"退出游戏",
                    "function":self.parent.GameQuit,
                    "pos":None,
                    "index":0
                }
            }
        # ui偏移量
        self.offX = 330
        self.offY = 50

        # 鼠标悬浮时
        self.currItem = None
        self.currIndex = 0

        self.fpsBg = GC.loadSurface("msgBG_1.png").copy()
        self.fpsBg = pygame.transform.smoothscale(self.fpsBg,(80,30))


    def render(self):
        """渲设置ui"""
        # 渲染帧率
        fpsX = self.parent.win_w - self.parent.GameFont.getTextWidth(self.parent.fpsStr,13) -10
        self.parent.win.blit(self.fpsBg,(fpsX-20,0))
        self.parent.GameFont.render_lineText(fpsX,5,self.parent.fpsStr,12)
        # uiText =  f"音效:{self.parent.GameMusic.isPlaySound()} \n\n音效:{self.parent.GameMusic.isPlayBGM()}"
        uiText = "\n'X':打开怪物手册 \n'B':打开背包 \n'Esc':打开设置"
        self.parent.GameFont.render_multilineText(650,100,text=uiText,color=(127,255,0),TWidth=170,fontSize=14)
        if not self.isActive():
            return
        self.__render_view()
        # 监听事件
        self.__events()

    def __render_view(self):
        # 页面主体背景
        self.parent.win.blit(self.packBG,(self.offX,self.offY))
        # 边框
        pygame.draw.rect(self.parent.win,(200,200,0),(self.offX,self.offY,200,300),width=2)

        # 鼠标悬浮的背景
        # 按钮组
        for index,btn in enumerate(self.BtnGroups):
            x = self.offX+(200 - (len(self.BtnGroups[btn]["name"])*14)) //2
            y = self.offY + (index *14) +( index * 10) + 20
            self.parent.GameFont.render_lineText(x,y,text=self.BtnGroups[btn]["name"],color=(255,255,255),fontSize=14)
            self.BtnGroups[btn]["pos"] = (x,y)   # 鼠标悬浮边框坐标
            self.BtnGroups[btn]["index"] = index  # 当前按钮的索引
        
        # 显示对应开关的状态
        self.parent.GameFont.render_lineText(495,self.BtnGroups["closeDuel"]["pos"][1],text=self.parent.GameDuel.isDuelAnim(),color=(255,50,50),fontSize=14)
        self.parent.GameFont.render_lineText(495,self.BtnGroups["changeSound"]["pos"][1],text=self.parent.GameMusic.isPlaySound(),color=(255,50,50),fontSize=14)
        self.parent.GameFont.render_lineText(495,self.BtnGroups["changeMusic"]["pos"][1],text=self.parent.GameMusic.isPlayBGM(),color=(255,50,50),fontSize=14)
        
        # 鼠标悬浮边框
        if self.currItem is not None:
            pygame.draw.rect(self.parent.win,(200,200,200),(self.currItem["pos"][0]-5,self.currItem["pos"][1]-2,self.parent.GameFont.getTextWidth(self.currItem["name"],14),24),width=2)
            self.parent.GameFont.render_lineText(self.currItem["pos"][0],self.currItem["pos"][1],text=self.currItem["name"],color=(255,100,0),fontSize=14)

    def __events(self):
        pos = pygame.mouse.get_pos()
        # 设置ui区域
        # self.currItem = None
        if pos[0] >= self.offX and pos[0] < self.offX+200 and pos[1] >= self.offY+20 and pos[1] < self.offY+300:
            for i in self.BtnGroups:
                if pos[1] >= self.BtnGroups[i]["pos"][1] and pos[1] < self.BtnGroups[i]["pos"][1] +24:
                    self.currItem = self.BtnGroups[list(self.BtnGroups.keys())[self.BtnGroups[i]["index"]]]
                    break
        else:
            self.currItem = self.BtnGroups[list(self.BtnGroups.keys())[self.currIndex]]

        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.parent.GameQuit()
            elif e.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed() == (1,0,0):
                    if pos[0] >= self.offX and pos[0] < self.offX+200 and pos[1] >= self.offY+20 and pos[1] < self.offY+300:
                        self.currItem["function"]()

                
            elif e.type == pygame.KEYDOWN:
                if e.key == pygame.K_UP:
                    self.currIndex -= 1
                    if self.currIndex < 0:
                        self.currIndex = len(self.BtnGroups.keys())-1
                elif e.key == pygame.K_DOWN:
                    self.currIndex += 1
                    if self.currIndex > len(self.BtnGroups.keys())-1:
                        self.currIndex = 0
                elif e.key == pygame.K_RETURN:
                    self.currItem["function"]()
                    
                elif e.key == K_b or e.key == K_ESCAPE:
                    self.ShowUI()

    def isActive(self):
        """返回背包是否激活状态"""
        return self.__isActive

    def ShowUI(self,active=None):
        """关闭设置"""
        if active is not None:
            self.__isActive = not active
        self.__isActive = not self.__isActive